home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 10 / REFCOUNT.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  4KB  |  106 lines

  1. // File from page 436 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: REFCOUNT.CPP -- Reference count, copy-on-write
  34. #include <string.h>
  35. #include <assert.h>
  36.  
  37. class counted {
  38.   class memblock {
  39.     enum { size = 100 };
  40.     char c[size];
  41.     int refcount;
  42.   public:
  43.     memblock() {
  44.       memset(c, 1, size);
  45.       refcount = 1;
  46.     }
  47.     memblock(const memblock& rv) {
  48.       memcpy(c, rv.c, size);
  49.       refcount = 1;
  50.     }
  51.     void attach() { ++refcount; }
  52.     void detach() {
  53.       assert(refcount != 0);
  54.       // Destroy object if no one is using it:
  55.       if(--refcount == 0) delete this;
  56.     }
  57.     int count() const { return refcount; }
  58.     void set(char x) { memset(c, x, size); }
  59.     // Conditionally copy this memblock.
  60.     // Call before modifying the block; assign
  61.     // resulting pointer to your block;
  62.     memblock* unalias() {
  63.       // Don't duplicate if not aliased:
  64.       if(refcount == 1) return this;
  65.       --refcount;
  66.       // Use copy-constructor to duplicate:
  67.       return new memblock(*this);
  68.     }
  69.   } * block;
  70. public:
  71.   counted() {
  72.     block = new memblock; // Sneak preview
  73.   }
  74.   counted(const counted& rv) {
  75.     block = rv.block; // Pointer assignment
  76.     block->attach();
  77.   }
  78.   void unalias() { block = block->unalias(); }
  79.   counted& operator=(const counted& rv) {
  80.     // Check for self-assignment:
  81.     if(&rv == this) return *this;
  82.     // Clean up what you're using first:
  83.     block->detach();
  84.     block = rv.block; // Like copy-constructor
  85.     block->attach();
  86.     return *this;
  87.   }
  88.   // Decrement refcount, conditionally destroy
  89.   ~counted() { block->detach(); }
  90.   // Copy-on-write:
  91.   void write(char value) {
  92.     // Do this before any write operation:
  93.     unalias();
  94.     // It's safe to write now.
  95.     block->set(value);
  96.   }
  97. };
  98.  
  99. main() {
  100.   counted A, B;
  101.   counted C(A);
  102.   B = A;
  103.   C = C;
  104.   C.write('x');
  105. }
  106.